package com.xypower.link;

public class FindFirstIntersectNode {
    public static class Node{
        public int value;
        public Node next;
        public Node(int data) {
            this.value = data;
        }
    }

    public static Node getIntersectNode(Node head1,Node head2){
        if (head1 ==null || head2 == null) {
            return null;
        }
        Node loop1 = getLoopNode(head1);
        Node loop2 = getLoopNode(head2);
        if (loop1 == null && loop2 == null){
            return noLoop(head1,head2);
        }
        if (loop1 != null && loop2 != null){
            return bothLoop(head1,loop1,head2,loop2);
        }
        return null;
    }

    //两个链表都有环
    private static Node bothLoop(Node head1, Node loop1, Node head2, Node loop2) {
        Node cur1 = null;
        Node cur2 = null;
        if (loop1 == loop2){
            cur1 = head1;
            cur2 = head2;
            int n = 0;
            while (cur1 != loop1){
                n++;
                cur1 = cur1.next;
            }
            while (cur2 != loop2){
                n--;
                cur2 = cur2.next;
            }
            cur1 = n > 0 ? head1 : head2;//谁长谁变为cur1
            cur2 = cur1 == head1 ? head2 : head1;//谁短谁变为cur2
            n = Math.abs(n);
            //长链表先走差值步
            while(n!=0){
                n--;
                cur1 = cur1.next;
            }
            //一起走
            while (cur1 != cur2){
                cur1 = cur1.next;
                cur2 = cur2.next;
            }
            return cur1;
        }else {
            //loop1往下走，在回到loop1之前，相遇loop2则相交，返回loop1或者loop2
            cur1 = loop1.next;
            while (cur1!=loop1){
                if (cur1 == loop2){
                    return loop1;
                }
                cur1 = cur1.next;
            }
            return null;
        }
    }

    //两个链表都无环
    private static Node noLoop(Node head1, Node head2) {
        if (head1 == null || head2 ==null){
            return null;
        }
        Node cur1 = head1;
        Node cur2 = head2;
        int n=0;
        while (cur1.next !=null){
         n++;
         cur1 = cur1.next;
        }
        while (cur2.next!=null){
            n--;
            cur2 = cur2.next;
        }
        //最后n为len1与len2的差值
        //end1不等于end2，则两个链表不相交
        if (cur1 != cur2){
            return null;
        }
        cur1 = n > 0 ? head1 : head2;//谁长谁变为cur1
        cur2 = cur1 == head1 ? head2 : head1;//谁短谁变为cur2
        n = Math.abs(n);
        //长链表先走差值步
        while(n!=0){
            n--;
            cur1 = cur1.next;
        }
        //一起走
        while (cur1 != cur2){
            cur1 = cur1.next;
            cur2 = cur2.next;
        }
        return cur1;
    }

    //判断单链表有无环，找到链表的第一个入环节点，如果无环，返回null
    public static Node getLoopNode(Node head){
        if (head == null || head.next==null || head.next.next==null){
            return null;
        }
        //快慢指针，快指针走一步，慢指针走两步
        Node n1 = head.next;
        Node n2 = head.next.next;
        while (n1 != n2){
            if (n2.next==null|| n2.next.next == null){
                return null;
            }
            n2 = n2.next.next;
            n1 = n1.next;
        }
        //相遇后，快指针回到head，快慢指针开始同时一步一走，再次相遇的第一个结点为入环结点
        n2 = head;
        while (n1 != n2){
            n1 = n1.next;
            n2 = n2.next;
        }
        return n1;
    }
}
